import {
  getPagePath,
  getCurPageQuery,
  getPagesConfig,
  isTabBarPage
} from './sys'
import { str2param, param2str } from './url'
import { isPureObject } from './validate'

import { useAppStore } from '@/store/modules/app'
const store = useAppStore()

/**
 * @description: 获取页面路由参数
 * @return {Object}
 * object => {
 *  path: 路径,
    query: 路径参数,
    fullPath: 全路径,
    title: 标题
 * }
 * @author: dubangrong
 */
export const getCurRouter = function () {
  let query = getCurPageQuery()
  let title = getPagesConfig()
  let page = getPagePath(false)
  return {
    path: page.route,
    query,
    fullPath: page.$page && page.$page.fullPath,
    title: title?.style?.navigationBarTitleText || ''
  }
}

export const navTo = function (index, _url = '', fun = '') {
  let query = ''
  let query2 = {}
  let url = _url

  if (isPureObject(_url)) {
    let url2 = _url.path || ''
    query2 = _url.query || {}
    url = url2
  }

  query = param2str(
    {
      ...str2param(url),
      ...query2
    },
    true
  )

  if (url && typeof url !== 'number') {
    let path = url.indexOf('?') != -1 ? url : url + query
    if (url[0] === '/') {
      url = path
    } else {
      url = '/' + path
    }
  }

  // #ifndef H5
  //   除了H5  其他判断是否跳转的路径是tabbar页面
  //   if (isTabBarPage(url)) {
  //     index = 3
  //   }
  // #endif
  //   跳转成功执行
  const success = () => {
    if (typeof fun === 'function') {
      fun()
    }
  }

  if (index === 1) {
    // 关闭当前页，返回上一页面或多级页面。
    uni.navigateBack({
      delta: url || 1,
      animationType: 'pop-out',
      animationDuration: 300
    })
  } else if (index === 2) {
    // 保留当前页，跳转到非tabbar页面，使用uni.navigateBack可以返回到原页面。
    if (getCurrentPages().length >= 10) {
      uni.redirectTo({
        url,
        fail: () => {
          //   uni.navigateTo({
          //     url: '/pages/404/404',
          //     animationType: 'pop-in',
          //     animationDuration: 300
          //   })
        },
        success
      })
    } else {
      uni.navigateTo({
        url,
        animationType: 'pop-in',
        animationDuration: 300,
        fail: (err) => {
          //   uni.navigateTo({
          //     url: '/pages/404/404',
          //     animationType: 'pop-in',
          //     animationDuration: 300
          //   })
        },
        success
      })
    }
  } else if (index === 3) {
    // 跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面。
    uni.switchTab({
      url,
      animationType: 'pop-in',
      animationDuration: 300,
      success
    })
  } else if (index === 4) {
    // 关闭所有页面，打开到应用内的某个页面。
    uni.reLaunch({
      url,
      animationType: 'pop-in',
      animationDuration: 300,
      success
    })
  } else if (index === 5) {
    // 关闭当前页面，跳转到应用内的某个页面。
    uni.redirectTo({
      url,
      fail: () => {
        uni.navigateTo({
          url: '/pages/404/404',
          animationType: 'pop-in',
          animationDuration: 300
        })
      },
      success
    })
  }
}

//重定向当前页
export const navToCurrentPage = function (query = {}) {
  const pages = getCurrentPages()
  const page = pages[pages.length - 1]
  const path = '/' + page.route
  navTo(5, { path, query })
}

// 返回上一页
export const goBack = function () {
  let canNavBack = getCurrentPages()
  if (canNavBack && canNavBack.length > 1) {
    uni.navigateBack({
      delta: 1
    })
  } else {
    uni.reLaunch({
      url: '/pages/index/index'
    })
  }
}

// 跳回登录前的页面
export const navBackAfterLogin = function (url) {
  let { redirectLoginUrl } = store
  if (
    (url && url !== 'undefined') ||
    (redirectLoginUrl && redirectLoginUrl !== 'undefined')
  ) {
    navTo(5, decodeURIComponent(decodeURIComponent(url || redirectLoginUrl)))
  } else {
    goBack()
  }
}
